'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: api_tool_handler.py
* @Time: 2025/11/11
* @All Rights Reserve By Brtc
'''
import uuid
from dataclasses import dataclass

from flask import request
from flask_login import current_user
from injector import inject

from internal.schema.api_tool_schema import ValidateOpenAPISchema, CreateApiToolReq, GetApiToolProviderWithPageReq, \
    GetApiToolProvidersWithPageRsp, UpdateApiToolProviderReq, GetApiToolProviderResp, GetApiToolResp
from internal.service import ApiToolService
from pkg.paginator.paginator import PageModel
from pkg.response.response import validate_error_json, success_message, success_json


@inject
@dataclass
class ApiToolHandler:
    """自定义 api 插件处理"""
    api_tool_service: ApiToolService


    def validate_openapi_schema(self):
        """校验传递的openapi_schema字符串是否正确"""
        req = ValidateOpenAPISchema()

        if not req.validate():
            return validate_error_json(req.errors)

        self.api_tool_service.parse_openapi_schema(req.openapi_schema.data)
        return success_message("数据校验成功！！！")


    def create_api_tool_provider(self):
        """创建自定义工具提供商"""
        req = CreateApiToolReq()
        if not req.validate():
            return validate_error_json(req.errors)

        self.api_tool_service.create_api_tool(req, current_user)
        return success_message("创建自定义API插件提供商成功！！")


    def get_api_tool_providers_with_page(self):
        """获取API工具提供者列表信息, 该接口支持分页"""
        req = GetApiToolProviderWithPageReq(request.args)
        if not req.validate():
            return validate_error_json(req.errors)

        api_tool_providers, paginator = self.api_tool_service.get_api_tool_providers_with_page(req)

        resp = GetApiToolProvidersWithPageRsp(many=True)
        return success_json(PageModel(list = resp.dump(api_tool_providers), paginator = paginator))


    def update_api_tool_provider(self, provider_id:uuid.UUID):
        """更新自定义API工具提供者信息(思路是 先删除后增加)"""
        req = UpdateApiToolProviderReq()
        if not req.validate():
            return validate_error_json(req.errors)

        self.api_tool_service.update_api_tool_provider(provider_id, req)
        return success_message("更新自定义API 插件成功！！！")


    def get_api_tool_provider(self, provider_id: uuid.UUID):
        """根据传递的provider_id获取工具提供者的原始信息"""
        api_tool_provider = self.api_tool_service.get_api_tool_provider(provider_id)
        resp = GetApiToolProviderResp()
        return success_json(resp.dump(api_tool_provider))


    def get_api_tool(self, provider_id: uuid.UUID, tool_name: str):
        """根据传递的provider_id+tool_name获取工具的详情信息"""
        api_tool = self.api_tool_service.get_api_tool(provider_id, tool_name)
        resp = GetApiToolResp()
        return success_json(resp.dump(api_tool))


    def delete_api_tool_provider(self, provider_id: uuid.UUID):
        """根据传递的provider_id删除对应的工具提供者信息"""
        self.api_tool_service.delete_api_tool_provider(provider_id)
        return success_message("删除自定义API插件成功")
